/*
 *    Copyright (c) 2018-2025, daoism All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: daoism
 */
package com.mkh.st.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mkh.st.admin.api.dto.DocTypePageDTO;
import com.mkh.st.admin.api.entity.SysDocType;
import com.mkh.st.admin.api.entity.SysPermissionScope;
import com.mkh.st.admin.api.vo.DocTypePageVO;
import com.mkh.st.admin.mapper.SysDocTypeMapper;
import com.mkh.st.admin.mapper.SysPermissionScopeMapper;
import com.mkh.st.admin.service.SysDocTypeService;
import com.mkh.st.common.core.constant.CommonConstants;
import com.mkh.st.common.core.util.R;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 单据类型表
 *
 * @author daoism code generator
 * @date 2021-07-06 09:27:05
 */
@Service
@AllArgsConstructor
public class SysDocTypeServiceImpl extends ServiceImpl<SysDocTypeMapper, SysDocType> implements SysDocTypeService {

	private final SysDocTypeMapper sysDocTypeMapper;
	private final SysPermissionScopeMapper sysPermissionScopeMapper;
	private final Map<Long,String> scopeMap=new ConcurrentHashMap<>();

    @Override
    public R<IPage<DocTypePageVO>> docTypePage(Page page, DocTypePageDTO dto) {

		IPage<SysDocType> iPage=sysDocTypeMapper.selectPage(page, Wrappers.<SysDocType>lambdaQuery()
				.like(StrUtil.isNotBlank(dto.getCode()),SysDocType::getCode,dto.getCode())
				.like(StrUtil.isNotBlank(dto.getName()),SysDocType::getName,dto.getName()));
		IPage<DocTypePageVO> pageInfo=new Page<>();
		if(CollectionUtil.isEmpty(iPage.getRecords())){
			return R.ok(pageInfo);
		}
		List<DocTypePageVO> list=new ArrayList<>();
		iPage.getRecords().forEach(item ->{
			DocTypePageVO vo= BeanUtil.toBean(item,DocTypePageVO.class);
			List<Long> ids = Convert.toList(Long.class, StrUtil.split(item.getScopeIds(), ','));
			StringBuilder str=new StringBuilder();
			for (Long id : ids) {
				if(scopeMap.containsKey(id)){
					str.append(","+scopeMap.get(id));
				}else {
					SysPermissionScope sysPermissionScope = sysPermissionScopeMapper.selectById(id);
					str.append(","+sysPermissionScope.getScopeName());
					scopeMap.put(id,sysPermissionScope.getScopeName());
				}
			}
			vo.setDocScopes(str.toString().substring(CommonConstants.ONE));
			list.add(vo);
		});
		pageInfo.setRecords(list).setTotal(iPage.getTotal()).setSize(iPage.getSize()).setCurrent(iPage.getCurrent()).setPages(iPage.getPages());
        return R.ok(pageInfo);
    }

}
